﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JSIF_WCFServer.Server;
using System.Security.Principal;
using System.Diagnostics;

namespace JSIF_WCFServer.UI
{
    /// <summary>
    /// GUI container for Bookshelf server
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            initServer();
        }

        private void initServer()
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(delegate(Object ignored)
            {
                try
                {
                    _server = new BookshelfServer();

                    //System.IO.DirectoryInfo repository = new System.IO.DirectoryInfo(Application.StartupPath + System.IO.Path.DirectorySeparatorChar + "repository");
                    //System.IO.DirectoryInfo working = new System.IO.DirectoryInfo(Application.StartupPath + System.IO.Path.DirectorySeparatorChar + "working");
                    //JSIF_WCFServer.VersionControl.SVNVersionControl svn = new JSIF_WCFServer.VersionControl.SVNVersionControl(repository, working);
                }
                catch (NeedElevatedPermissionException e)
                {
                    if (!RunningAsAdmin())
                    {
                        RunElevated();
                    }
                    else
                    {
                        MessageBox.Show("Not able to start the bookshelf server\n" + e);
                    }
                }
            }));
        }

        private BookshelfServer _server;

        private void HideWhenMinimized(object sender, EventArgs e)
        {
            ShowInTaskbar = WindowState != FormWindowState.Minimized;
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            showIfHidden();
        }

        private void showIfHidden()
        {
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
            }
            this.BringToFront();
            this.Focus();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            notifyIcon.Visible = false;
            try { _server.stop(); }
            catch { }
        }

        private bool RunningAsAdmin()
        {
            bool result = false;
            try
            {
                WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                result = principal.IsInRole(WindowsBuiltInRole.Administrator);
            }
            catch (Exception e)
            {
                System.Console.Error.WriteLine(e);
            }
            return result;
        }

        private void RunElevated()
        {
            ProcessStartInfo processInfo = new ProcessStartInfo();
            processInfo.Verb = "runas";
            processInfo.FileName = Application.ExecutablePath;
            Process.Start(processInfo);
            Application.Exit();
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void restartBookshelfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                _server.stop();
                _server.start();
            }
            catch { }
        }

        private void browseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            browseServer();
        }

        private void browseServer()
        {
            OpenURL(new Uri("http://localhost:" + _server.Port));
        }

        private static void OpenURL(Uri uri)
        {
            try
            {
                System.Diagnostics.Process.Start(uri.ToString());
            }
            catch { }
        }

        private void aboutJSIFBookshelfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.TabIndex = 0;
            showIfHidden();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.TabIndex = 1;
            showIfHidden();
        }

        private void debugConsoleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl1.TabIndex = 2;
            showIfHidden();
        }
    }
}
